source("~/Projects/NBI/R/convert2016.R")
nbi <- convert2016("~/Data/2016hwybronlyonefile.zip")
## Warning in convert2016("~/Data/2016hwybronlyonefile.zip"): NAs introduced
## by coercion

## Warning in convert2016("~/Data/2016hwybronlyonefile.zip"): NAs introduced
## by coercion

## Warning in convert2016("~/Data/2016hwybronlyonefile.zip"): NAs introduced
## by coercion
library(maps)
#library(MazamaSpatialUtils)

Traffic

summary(nbi$averageCarCount)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##       0     110     841    7532    5875  806650
hist(nbi$averageCarCount, n=100, main = "Distribution of daily bridge traffic")

Most bridges have low traffic, but daily traffic ranges from 0 to 806650 cars per day. Let’s look at some things which might explain that variation, starting with looking at how it is spatially distributed.

map("state")
points(nbi$longitude, nbi$latitude, pch = 17, cex = as.numeric(nbi$averageCarCount)/807000*6)
title("Bridge Traffic")

What kinds of bridges get the most traffic?

map("state")
with(dplyr::filter(nbi, water == 0), points(longitude, latitude, pch = 17, cex = averageCarCount/100000))

map("state")
with(dplyr::filter(nbi, water == 1), points(longitude, latitude, pch = 17, cex = averageCarCount/100000))

Most of the largest bridges are not over water, and are likely large highway interchanges or the like. There are many more bridges going over water than bridges that don’t go over water.

What kinds of pictures can we make with bridges?

map("state")
 with(dplyr::filter(nbi, water == 1), points(longitude, latitude, pch = 17, cex = .05))

 map("state")
  with(dplyr::filter(nbi, water == 0), points(longitude, latitude, pch = 17, cex = .1))

We get what looks like a map of major highways when we just map bridges that do not go over water. Let’s take a closer look at our favorite state.

wa <- dplyr::filter(nbi, stateCode == "WA")

map("state", "washington")
with(dplyr::filter(wa, water == 0), points(longitude, latitude, pch = 2, cex = .3))

map("state", "washington")
with(dplyr::filter(wa, water == 1), points(longitude, latitude, pch = 2, cex = .3))

In the first map we can see the major highways. I’m not sure what’s going on in the Kitsap peninsula, but everything else is generally as to be expected. The second map shows a completely different story. While we can still see the highways, they are more obscured. Instead, we can now make out the shapes of the rivers, with the lines of the skagit and skyhomish and other rivers draining into the Puget Sound. On the coast, we see lines of bridges marking rivers running into the ocean.

Out of the bridges that do span water, which are most at risk of flooding?

sum(wa$waterwayAdequacy == 2)
## [1] NA
map("state", "washington")
with(dplyr::filter(wa, water == 1), points(longitude, latitude, pch = 17, cex = .1))
with(dplyr::filter(wa, water == 1 & waterwayAdequacy == "2"), points(longitude, latitude, pch = 17, cex = 1, col = "red"))

map("state","wa")
with(subset(wa, channelCondition <= 7), points(longitude, latitude))

map("state","wa")
with(subset(wa, channelCondition > 7), points(longitude, latitude))

library(MazamaSpatialUtils)

setSpatialDataDir("~/Data/Spatial")
MazamaSpatialUtils::convertUSCensusCounties()
## OGR data source with driver: ESRI Shapefile 
## Source: "/Users/helen/Data/Spatial/counties", layer: "cb_2013_us_county_20m"
## with 3221 features
## It has 9 fields
## Integer64 fields read as strings:  ALAND AWATER
loadSpatialData("USCensusCounties")

stateColors <- RColorBrewer::brewer.pal(6,"PuRd")

waCounties <- subset(USCensusCounties, stateCode == "WA")
wa$county <- getUSCounty(wa$longitude, wa$latitude, dataset = "USCensusCounties", stateCode = "WA")
countyCondition <- aggregate(data = wa, channelCondition~county, FUN = "mean")
breaks <- seq(6,8, length = 7)
colorIndex <- .bincode(countyCondition$channelCondition, breaks = breaks)
names(colorIndex) <- countyCondition$county
plot(waCounties, col = stateColors[colorIndex[waCounties$countyName]])

#Multi-lane bridges
summary(nbi$laneCount)
summary(nbi$underLaneCount)
#most bridges have two lanes and pass over 0 lanes.
#what is up with bridges with 82 lanes, or passing over 99 lanes? 

table(nbi$laneCount)
table(nbi$underLaneCount)

bigBridges <- nbi$laneCount > 8
reallyBigBridges <- nbi$laneCount > 20 

map("state")
points(nbi$longitude[bigBridges], nbi$latitude[bigBridges], cex = nbi$laneCount[bigBridges]/15, pch = 2)
#Big bridges tend to be really clumped together. Most likely around big cities.
map("state")
points(nbi$longitude[reallyBigBridges], nbi$latitude[reallyBigBridges], cex = nbi$laneCount[reallyBigBridges]/25, pch = 2)
#What is going on in Tennessee?

#It looks like big bridges might follow similar patterns to the high-traffic bridges. Let's see.
map("state")
points(nbi$longitude, nbi$latitude, pch = 17, cex = as.numeric(nbi$averageCarCount)/807000*6)
points(nbi$longitude[bigBridges], nbi$latitude[bigBridges], cex = nbi$laneCount[bigBridges]/15, pch = 2, col = "red")


#There are a lot of really HUGE bridges in TN? Why? Let's see what's going on there. 
tn <- dplyr::filter(nbi, stateCode == "TN")
map("state", "ten")
points(tn$longitude, tn$latitude, pch = 17, cex = as.numeric(tn$averageCarCount)/807000*6)
points(nbi$longitude[bigBridges], nbi$latitude[bigBridges], cex = nbi$laneCount[bigBridges]/15, pch = 2, col = "red")
MazamaSpatialUtils::convertUSCensusCounties()
loadSpatialData("USCensusCounties")